package edu.chl.tda367.mynotes.core;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.swing.text.StyledDocument;
import javax.swing.undo.UndoManager;

/*
 * A class implementing the singleton pattern. Contains all the changes
 * that may occur. The view has a listener to update whenever the propertyChange
 * fires. MyNotes contains methods for getting and setting the styledDocument,
 * text and filename. Also contains methods to check if the document is saved.
 */

public class MyNotes {

	private final PropertyChangeSupport observers = new PropertyChangeSupport(this);
	private Document activeDocument;
	private static MyNotes instance = null;
	private StyledDocument doc;
	private String savedText;
	private String text;
	private String fileName;
	private boolean isChanged = false;
	private boolean isSaved = false;
	private UndoManager undoManager = new UndoManager();
	private String filePath;

	private MyNotes(){

	}

	/*
	 * A method implementing the singleton pattern.
	 */
	public static MyNotes getInstance(){
		if(instance == null){
			instance = new MyNotes();
		}
		return instance;
	}

	public void addObserver(PropertyChangeListener observer) {
		observers.addPropertyChangeListener(observer);
	}

	public void setNewDocument(Document d){
		this.activeDocument = d;
		observers.firePropertyChange("NewDoc", null, activeDocument);
	}

	public void setOpenDocument(Document d){
		this.activeDocument = d;
		observers.firePropertyChange("OpenDoc", null, activeDocument);
	}

	public void setSavedDocument() {
		observers.firePropertyChange("SaveDoc", null, activeDocument);
	}

	public void setBold() {
		observers.firePropertyChange("Bold", null, activeDocument);
	}

	public void setItalic() {
		observers.firePropertyChange("Italic", null, activeDocument);
	}

	public void setUnderline() {
		observers.firePropertyChange("Underline", null, activeDocument);
	}

	public void setStrikeThrough() {
		observers.firePropertyChange("StrikeThrough", null, activeDocument);
	}

	public void setSuperscript() {
		observers.firePropertyChange("Superscript", null, activeDocument);
	}

	public void setSubscript() {
		observers.firePropertyChange("Subscript", null, activeDocument);
	}

	public void cutText() {
		observers.firePropertyChange("Cut", null, activeDocument);
	}

	public void setFont() {
		observers.firePropertyChange("Font", null, activeDocument);
	}

	public void setSize() {
		observers.firePropertyChange("Size", null, activeDocument);
	}

	public void insertImage() {
		observers.firePropertyChange("Image", null, activeDocument);
	}

	public void copyText() {
		observers.firePropertyChange("Copy", null, activeDocument);
	}

	public void pasteText() {
		observers.firePropertyChange("Paste", null, activeDocument);
	}

	public void undoChanges() {
		observers.firePropertyChange("Undo", null, activeDocument);
	}

	public void redoChanges() {
		observers.firePropertyChange("Redo", null, activeDocument);
	}

	public void centerText() {
		observers.firePropertyChange("Center", null, activeDocument);
	}

	public void leftAlignText() {
		observers.firePropertyChange("Left", null, activeDocument);
	}

	public void rightAlignText() {
		observers.firePropertyChange("Right", null, activeDocument);
	}

	public void setColor() {
		observers.firePropertyChange("Color", null, activeDocument);
	}
	public void setBackgroundColor() {
		observers.firePropertyChange("BackgroundColor", null, activeDocument);
	}

	public Document getActiveDocument(){
		return activeDocument;
	}

	public void setDocument(StyledDocument sd) {
		this.doc = sd;
	}

	public StyledDocument getDocument(){
		return this.doc;
	}
	
	public void setSavedText(String s) {
		this.savedText = s;
	}

	public String getSavedText(){
		return savedText;
	}

	public void setText(String s) {
		this.text = s;
	}
	public String getText() {
		return text;
	}

	public void setFileName(String s) {
		this.fileName = s;
	}

	public String getFileName() {
		return fileName;
	}
	
	public String getFilePath() {
		return filePath;
	}
	
	public void setFilePath(String path) {
		this.filePath = path;
	}
	
	public void setIsChanged(boolean changed) {
		this.isChanged = changed;
	}
	
	public boolean getIsChanged() {
		return isChanged;
	}
	
	public void setIsSaved(boolean saved) {
		this.isSaved = saved;
	}
	
	public boolean getIsSaved() {
		return isSaved;
	}

	public UndoManager getUndoManager() {
		return undoManager;
	}
}
